<template>
	<view class="page">
		<flowbefore :beflows="beflows" :status="status" :isBtnClick="isBtnClicks" @save="save" @submit="submit" :statusType="statusType">
			<template>
				<view class="padding-bottom-100">
					<form class="flowform">
						<view class="formlist inputtext">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>流程标题</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="material.flowTitle" name="flowTitle" :disabled="disabled" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext ">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>单号编号</label></view>
								<view class="controlvalue">
									<input type="text" v-model="material.billNo" name="billNo" disabled="true" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="flowUrgent">
							<view class="formgroup">
								<view class="labletitle"><label>紧急程度</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="material.flowUrgent" name="flowUrgent" placeholder="选择" disabled="true" />
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist inputtext margin-top-20">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>领料人员</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="material.leadPeople" name="leadPeople" placeholder="输入领料人员" :disabled="disabled" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>领料部门</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="material.leadDepartment" name="leadDepartment" placeholder="输入领料部门" :disabled="disabled" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="leadDate">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>领料日期</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="material.leadDate" name="leadDate" placeholder="选择" disabled="true" />
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist inputtext">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>仓库</label></view>
								<view class="controlvalue">
									<input type="text" v-model="material.warehouse" name="warehouse" placeholder="输入仓库" :disabled="disabled" />
								</view>
							</view>
						</view>
						<view class="formlist">
							<view class="formgroup  padding-17">
								<view class="labletitletar"><label>备注</label></view>
								<view class="controlvalue uni-textarea">
									<textarea v-model="material.description" name="description" :disabled="disabled" placeholder="输入" />
									</view>
							</view>
						</view>
						<!-- #ifndef H5 -->
						<view class="uni-list" v-for="(item, key) in detailproList" :key="key">
						<!-- #endif -->
						<!-- #ifdef H5 -->
						<view class="uni-list" v-for="(item, key) in detailproList" :key="'detailpro'+key">
						<!-- #endif -->
							<view class="formgtitle">
								<view class="formgroup">
									<view class="labletitle font-size24"><label>商品添购({{key+1}})</label></view>
									<view v-if="detailproList.length>1&&status=='add'" class="controlvalue text-align-right font-size24"><text class="color-4869EE" @tap="removeDetailpro(key)">删除</text></view>
								</view>
							</view>
							<view class="formlist inputtext">
								<view class="formgroup border-bottom-E3E3E5">
									<view class="labletitle"><label>商品名称</label><text class="required">*</text></view>
									<view class="controlvalue">
										<input type="text" name="goodsName" v-model="item.goodsName" placeholder="输入商品名称" :disabled="disabled" />
									</view>
								</view>
							</view>
							<view class="formlist inputtext">
								<view class="formgroup border-bottom-E3E3E5">
									<view class="labletitle"><label>规格型号</label><text class="required">*</text></view>
									<view class="controlvalue">
										<input type="text" name="specifications" v-model="item.specifications" placeholder="输入规格型号" :disabled="disabled" />
									</view>
								</view>
							</view>
							<view class="formlist inputtext">
								<view class="formgroup border-bottom-E3E3E5">
									<view class="labletitle"><label>单位</label></view>
									<view class="controlvalue">
										<input type="text" name="unit" v-model="item.unit" placeholder="输入单位" :disabled="disabled" />
									</view>
								</view>
							</view>
							<view class="formlist inputtext">
								<view class="formgroup border-bottom-E3E3E5">
									<view class="labletitle"><label>配数量</label><text class="required">*</text></view>
									<view class="controlvalue">
										<input type="number" name="proportioning" v-model="item.proportioning" @blur="blurQty(key)" :disabled="disabled" maxlength="8" placeholder="输入配数量" />
									</view>
								</view>
							</view>
							<view class="formlist inputtext">
								<view class="formgroup border-bottom-E3E3E5">
									<view class="labletitle"><label>需数量</label><text class="required">*</text></view>
									<view class="controlvalue">
										<input type="number" name="materialDemand" v-model="item.materialDemand" :disabled="disabled" maxlength="8" placeholder="输入需数量" />
									</view>
								</view>
							</view>
							<view class="formlist inputtext">
								<view class="formgroup border-bottom-E3E3E5">
									<view class="labletitle"><label>单价</label><text class="required">*</text></view>
									<view class="controlvalue">
										<input type="digit" name="price" v-model="item.price" @blur="blurPrice(key)" :disabled="disabled" placeholder="输入单价" />
									</view>
								</view>
							</view>
							<view class="formlist inputtext">
								<view class="formgroup border-bottom-E3E3E5">
									<view class="labletitle"><label>金额</label><text class="required">*</text></view>
									<view class="controlvalue">
										<input type="digit" name="amount" v-model="item.amount" :disabled="disabled" placeholder="输入金额" />
									</view>
								</view>
							</view>
							<view class="formlist">
								<view class="formgroup  padding-17">
									<view class="labletitletar"><label>备注</label></view>
									<view class="controlvalue uni-textarea">
										<textarea v-model="material.description" name="description" :disabled="disabled" placeholder="输入备注" />
										</view>
								</view>
							</view>
						</view>
						<view v-if="status=='add'" class="formlist inputtext" @tap="addDetailpro">
							<view class="formgroup">
								<view class="second-title color-4869EE"><label class="iconfont icon-jia font-size28"></label>商品添购</view>
							</view>
						</view>
					</form>
					<view class="height-100"></view>
				</view>
			</template>
		</flowbefore>
		<w-picker :mode="mode" :pickerList="pickerList" ref="picker" :isSearchShow="isSearchShow"></w-picker>
	</view>
</template>

<script>
	import config from '@/common/js/config.js'
	import jnpf from '@/common/js/jnpf.js'
	import helper from '@/common/js/helper.js'
	import graceChecker from '@/common/js/graceChecker.js'
	import wPicker from '@/components/w-picker/w-picker.vue'
	import flowbefore from '@/components/work/flow-before.vue'
	let formRule=[
			{name:"flowTitle",checkType:"notnull",checkRule:"",errorMsg:"输入标题"},
			{name:"leadDepartment",checkType:"notnull",checkRule:"",errorMsg:"输入领料部门"},
			{name:"leadPeople",checkType:"notnull",checkRule:"",errorMsg:"输入领料人员"},
			{name:"flowUrgent", checkType : "notnull", checkRule:"",  errorMsg:"选择紧急程度"},
			{name:"leadDate", checkType : "notnull", checkRule:"",  errorMsg:"选择领料日期"},
		];
	let detailproRule=[
			{name:"goodsName",checkType:"notnull",checkRule:"",errorMsg:"输入商品名称"},
			{name:"specifications", checkType : "notnull", checkRule:"",  errorMsg:"输入商品规格"},
			{name:"proportioning", checkType : "int", checkRule:"0,8",  errorMsg:"配数量应为1-8个字符"},
			{name:"materialDemand", checkType : "int", checkRule:"0,8",  errorMsg:"需数量应为1-8个字符"},
			{name:"price", checkType : "money", checkRule:"",  errorMsg:"输入合格单价"},
			{name:"amount", checkType : "money", checkRule:"",  errorMsg:"输入合格金额"},
		]
	export default {
		data() {
			return {
				mode:'',//底部弹窗分类
				isSearchShow:false,
				disabled:false,//启用
				status:'',//流程状态
				beflows:{
					operstate:"",
					processId:''
				},
				material:{
					billNo:'',
					flowUrgent:'',
					flowTitle:jnpf.userInfo().realName+'的领料单',
					leadDate:'',
					leadDepartment:'',
					leadPeople:''
				},
				detailproList:[],
				isBtnClicks:false,
				pickerList:[],
				urgentList:[{
					label: "普通",
					value:"1"
				},
				{
					label: "重要",
					value:"2"
				},
				{
					label: "紧急",
					value:"3"
				}],
				sopping:config.Resources.common.sopping,
				statusType:''
			}
		},
		onLoad(e) {
			/* 工作区进入状态 */
			let statusType = uni.getStorageSync("type")
			this.statusType = statusType
			/* 工作区进入状态 End*/
			let flowInfo= JSON.parse(e.flowInfo);
			this.beflows.operstate=flowInfo.operstate;
			if(flowInfo.operstate =='add'){
				this.material.flowId=flowInfo.id;
				this.status = 0;
				this.material.flowUrgent=helper.getSpinnerNew(this.urgentList,'1');
				helper.ajax({
					url: '/api/System/BillRule/BillNumber/WF_MaterialRequisitionNo',
					success: res => {
						this.material.billNo=res.data.data;
					}
				});
				this.detailproList.push({
					goodsName:'',
					specifications:'',
					unit:'',
					proportioning:'',
					materialDemand:'',
					price:'',
					amount:'',
					description:''
				});
			}
			if(flowInfo.operstate=='info'){
				this.disabled=flowInfo.disabled;
				this.beflows.processId=flowInfo.id;
				this.status=flowInfo.status;
				helper.ajax({
					url: '/api/WorkFlowForm/MaterialRequisition/' + flowInfo.id,
					success: res => {
						let _material=res.data.data;
						this.material = _material;
						this.material.flowUrgent=helper.getSpinnerNew(this.urgentList,_material.flowUrgent);
						this.material.leadDate=helper.toDate(_material.leadDate, "yyyy-MM-dd HH:mm");
						this.detailproList = _material.entryList;
					}
				});
			}
		},
		methods: {
			flowUrgent(e){
				if(!this.disabled){
					this.pickerList = this.urgentList;
					this.mode="selector";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.material.flowUrgent = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			leadDate(e){
				if(!this.disabled){
					this.mode="dateTime";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.material.leadDate = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			blurQty(index){
				this.detailproList[index].amount=(this.detailproList[index].price*this.detailproList[index].proportioning).toFixed(2);
			},
			blurPrice(index){
				this.detailproList[index].amount=(this.detailproList[index].proportioning*this.detailproList[index].price).toFixed(2);
			},
			addDetailpro(){
				this.detailproList.push({
					goodsName:'',
					specifications:'',
					unit:'',
					proportioning:'',
					materialDemand:'',
					price:'',
					amount:'',
					description:''
				});
			},
			removeDetailpro(e){
				if(!this.disabled){
					this.detailproList.splice(e,1);
				}
			},
			submit(e){
				let formData=this.material;
				if (!graceChecker.check(formData,formRule)) {
					helper.msg(graceChecker.error);
					return
				}
				for(let i=0;i<this.detailproList.length;i++ ){
					if (!graceChecker.check(this.detailproList[i], detailproRule)) {
						helper.msg(graceChecker.error);
						return;
					}
				}
				this.isBtnClicks=true;
				formData.flowUrgent=helper.setSpinnerNew(this.urgentList,formData.flowUrgent);
				formData.status = 0;
				let method = "POST";
				let url = "/api/WorkFlowForm/MaterialRequisition";
				if(formData.id){
					method = "PUT";
					url=url+"/"+formData.id
				}
				formData.leadDate = Date.parse(formData.leadDate);
				formData.entryList = this.detailproList;
				helper.ajax({
					url: url,
					method:method,
					contentType: "application/json",
					data:formData,
					success: res => {
						helper.msg(res.data.msg, 'success');
						if (this.beflows.operstate=='add') {
							uni.redirectTo({
								url: '../../operate/started/started',
								success: res => {},
								fail: () => {},
								complete: () => {}
							});
						}else{
							uni.navigateBack();
						}
						this.isBtnClicks=false
					}
				});
			},
			save(e){
				let formData=this.material;
				if (!graceChecker.check(formData,formRule)) {
					helper.msg(graceChecker.error);
					return
				}
				for(let i=0;i<this.detailproList.length;i++ ){
					if (!graceChecker.check(this.detailproList[i], detailproRule)) {
						helper.msg(graceChecker.error);
						return;
					}
				}
				this.isBtnClicks=true;
				formData.flowUrgent=helper.setSpinnerNew(this.urgentList,formData.flowUrgent);
				formData.status = 1;
				let method = "POST";
				let url = "/api/WorkFlowForm/MaterialRequisition";
				if(formData.id){
					method = "PUT";
					url=url+"/"+formData.id
				}
				formData.leadDate = Date.parse(formData.leadDate);
				formData.entryList = this.detailproList;
				helper.ajax({
					url: url,
					method:method,
					contentType: "application/json",
					data:formData,
					success: res => {
						helper.msg(res.data.msg, 'success');
						if (this.beflows.operstate=='add') {
							uni.redirectTo({
								url: '../../operate/started/started',
								success: res => {},
								fail: () => {},
								complete: () => {}
							});
						}else{
							uni.navigateBack();
						}
						this.isBtnClicks=false
					}
				});
			},
		},
		components: {
			wPicker,
			flowbefore,
		}
	}
</script>

<style>
	.required{
		color: red;
	}
</style>
